package jt.classic.app.addressbook;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleManager;

import org.apache.taglibs.standard.lang.jpath.example.Person;
import org.limp.basework.SimpleBean;
import org.limp.basework.impl.SimpleBeanImpl;
import org.limp.basework.tools.BaseworkUtil;

public class AddressBookManager extends SimpleManager {
	public AddressBookManager(Connection conn) {
		super(conn);
	}

	
	/**
	 * 根据用户id获取该用户所有通讯录分类
	 * @param userId
	 * @return
	 */
	public List getPerNodeList(String userId) {
		List resultList = new ArrayList();
		String sql = "select  * from app_addrbook_category where userid = '"
				+ userId + "'";
		resultList = this.trimmer.searchMultiData(sql);

		return resultList;
	}

	public boolean addPersonAddrBookCatgory(String catName, String userId) {
		SimpleBean sb = new SimpleBeanImpl();

		sb.getResource().put("cname", catName);

		sb.getResource().put("userId", userId);

		BaseworkUtil util = new BaseworkUtil();

		boolean rnt = false;

		try {
			util.manualSave("APP_ADDRBOOK_CATEGORY", sb, this.conn);
			rnt = true;
		} catch (Exception ex) {
			ISystem.catchException(this, ex);
			rnt = false;
		}

		return rnt;
	}
	


	/**
	 * 获取指定类别的通讯录记录
	 * 
	 * @param id
	 * @return
	 */
	public List<Map> getPerList(String userId, String catId) {
		List resultList = new ArrayList();
		String sql = "select  * from app_addrbook where userid = '" + userId
				+ "' and catid = '" + catId + "'";
		List<Map> queryList = this.trimmer.searchMultiData(sql);

		for (Map m : queryList) {
			Map map = new HashMap();
			map.put("id", m.get("ID"));
			map.put("data",
					new String[] { (String) m.get("NAME"),
							(String) m.get("OFFICEPHONE"),
							(String) m.get("OFFICEFAX") });
			resultList.add(map);
		}

		return resultList;
	}
	
	/**
	 * 根据分类id和用户id删除所有属于该类别的记录
	 * @param catId
	 * @param userId
	 * @return
	 */
	public boolean delPerAddrBookByCat(String catId, String userId) {
		String sql = "delete from app_addrbook where catid = '" + catId
				+ "' and userid ='" + userId + "'";

		return this.trimmer.execute(sql);
	}

	/**
     * 删除联系人
     *
     * @param ids 联系人ID
     */
    public void deleteS(String ids) {
        String[] str = ids.split(",");
        String sql = null;

        for (int i = 0; i < str.length; i++) {
            if (!str[i].equals("")) {
                sql = "DELETE FROM APP_ADDRBOOK WHERE ID = '" + str[i] +"'";

                this.trimmer.executeBatch(sql);
            }
        }
    }
	/**
	 * 生成个人通讯录dhtmlTree的xml文件
	 * 
	 * @param parentid
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public String createPerDHTMLXML(List list) throws Exception {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

		buffer.append("<tree id=\"0\">");

		// 循环读取所有节点
		for (int i = 0; i < list.size(); i++) {
			Map m = (Map) list.get(i);

			buffer.append("<item text= \"" + m.get("CATNAME") + "\"");
			buffer.append(" id= \"" + m.get("ID") + "\"");

			buffer.append(" im0=\"leaf.gif\"");
			buffer.append(" child=\"0\" >");

			buffer.append("</item>");
		}

		buffer.append("</tree>");

		return buffer.toString();
	}
}
